Ejercicio 3¶

In [26]:
%%HTML
<script src="require.js"></script>
In [28]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
from plotly import graph_objects as go
import plotly.io as pio
pio.renderers.default = "notebook_connected"
In [ ]:
datos = pd.read_excel('games_sales_data_PBI.xlsx', sheet_name='games_sales_data_PBI')
#datos.dropna(inplace=True)
datos.head()

print(datos['Year_of_Release'].unique())
[2006. 1985. 2008. 2009. 1996. 1989. 1984. 2005. 1999. 2007. 2010. 2013.
 2004. 1990. 1988. 2002. 2001. 2011. 1998. 2015. 2012. 2014. 1992. 1997.
 1993. 1994. 1982. 2016. 2003. 1986. 2000.   nan 1995. 1991. 1981. 1987.
 1980. 1983. 2020. 2017.]
¿Cómo es la distribución porcentual de las ventas para los años 2000, 2005, 2010 y 2015?
In [7]:
datos['percentaje_sales'] = (datos['Global_Sales']* 100 / datos['Global_Sales'].sum()) 
datos_anos = datos[datos['Year_of_Release'].isin([2000, 2005, 2010, 2015])]

porcentajes_por_ano = datos.groupby('Year_of_Release')['percentaje_sales'].sum().reset_index()

porcentajes_anos_seleccionados = datos_anos.groupby('Year_of_Release')['percentaje_sales'].sum().reset_index()

print(porcentajes_anos_seleccionados)
   Year_of_Release  percentaje_sales
0           2000.0          2.259789
1           2005.0          5.137832
2           2010.0          6.620741
3           2015.0          3.004944
In [8]:
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(30, 25))
ax = fig.add_subplot(222)

# Barras para todos los años
bars1 = plt.bar(porcentajes_por_ano['Year_of_Release'], porcentajes_por_ano['percentaje_sales'], label='Todos los años', color='#3682BA')

# Barras para los años seleccionados (2020 y 2005)
bars2 = plt.bar(porcentajes_anos_seleccionados['Year_of_Release'], porcentajes_anos_seleccionados['percentaje_sales'], color='#FA9B58', label='Años 2020 y 2005')

# Agregar valores a las barras
for bar in bars1:
    height = bar.get_height()
    if height > 0:
        plt.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.2f}', 
                 ha='center', va='bottom', fontsize=10, color='black')

for bar in bars2:
    height = bar.get_height()
    if height > 0:
        plt.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.2f}', 
                 ha='center', va='bottom', fontsize=10, color='black')

plt.xlabel('Año')
plt.ylabel('Ventas Globales')
plt.title('Distribución de Ventas Globales por Año')
plt.legend()
plt.show()
No description has been provided for this image
In [9]:
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(25, 20))
ax = fig.add_subplot(222)

# Barras para todos los años
bars1 = plt.barh(porcentajes_por_ano['Year_of_Release'], porcentajes_por_ano['percentaje_sales'], label='Todos los años', color='#3682BA')

# Barras para los años seleccionados (2020 y 2005)
bars2 = plt.barh(porcentajes_anos_seleccionados['Year_of_Release'], porcentajes_anos_seleccionados['percentaje_sales'], color='#FA9B58', label='Años seleccionados')

# Agregar valores a las barras
for bar in bars1:
    width = bar.get_width()  # Longitud de la barra en eje X
    if width > 0:
        plt.text(width + 0.01, bar.get_y() + bar.get_height()/2, f'{width:.2f}', 
                 ha='left', va='center', fontsize=10, color='black')

for bar in bars2:
    width = bar.get_width()
    if width > 0:
        plt.text(width + 0.01, bar.get_y() + bar.get_height()/2, f'{width:.2f}', 
                 ha='left', va='center', fontsize=10, color='black')

plt.ylabel('Año')
plt.xlabel('Porcentaje de Ventas Globales')
plt.title('Distribución del Porcentaje de Ventas Globales por Año')
plt.legend()
plt.show()
No description has been provided for this image

¿Qué género aportó más porcentualmente en cada año y por cuánto valor?

In [17]:
datos_generos = datos.groupby('Genre')['Global_Sales'].sum().reset_index()
datos_generos['percentaje_sales'] = (datos_generos['Global_Sales']* 100 / datos_generos['Global_Sales'].sum())
datos_generos = datos_generos.sort_values(by='percentaje_sales', ascending=True)

datos_generos
Out[17]:
Genre Global_Sales percentaje_sales
11 Strategy 174.50 1.956743
1 Adventure 237.69 2.665320
5 Puzzle 243.02 2.725087
9 Simulation 390.42 4.377946
2 Fighting 447.48 5.017784
6 Racing 728.90 8.173467
3 Misc 803.18 9.006401
4 Platform 828.08 9.285615
7 Role-Playing 934.40 10.477827
8 Shooter 1052.94 11.807066
10 Sports 1332.00 14.936285
0 Action 1745.27 19.570458
In [18]:
import matplotlib.pyplot as plt

colors = plt.cm.Spectral(np.linspace(0, 1, len(datos_generos)))

fig = plt.figure(figsize=(25, 20))
ax = fig.add_subplot(222)

# Barras con colores variados
bars1 = plt.barh(datos_generos['Genre'], datos_generos['percentaje_sales'], color=colors, label='Todos los géneros', edgecolor='black')

# Agregar valores a las barras
for bar in bars1:
    width = bar.get_width()  # Longitud de la barra en eje X
    if width > 0:
        plt.text(width + 0.05, bar.get_y() + bar.get_height()/2, f'{width:.2f}', 
                 ha='left', va='center', fontsize=10, color='black')

plt.ylabel('Género')
plt.xlabel('Porcentaje de Ventas Globales')
plt.title('Distribución del Porcentaje de Ventas Globales por Género')

plt.show()
No description has been provided for this image

Compare 3 géneros en un rango de 4 años entre el 2000 al 2010 e indique ¿dónde presentó cada género mayor auge o ventas?

In [19]:
datos_temporales = datos[['Year_of_Release', 'Global_Sales', 'Genre']]
datos_temporales_wide =  datos_temporales.pivot_table(index='Year_of_Release', columns='Genre', values='Global_Sales', aggfunc='sum').fillna(0).reset_index()

#datos_temporales_wide.columns.name = None
datos_temporales_wide
Out[19]:
Genre Year_of_Release Action Adventure Fighting Misc Platform Puzzle Racing Role-Playing Shooter Simulation Sports Strategy
0 1980.0 0.34 0.00 0.77 2.71 0.00 0.00 0.00 0.00 7.07 0.00 0.49 0.00
1 1981.0 14.84 0.00 0.00 0.00 6.93 2.24 0.48 0.00 10.04 0.45 0.79 0.00
2 1982.0 6.52 0.00 0.00 0.87 5.03 10.03 1.57 0.00 3.79 0.00 1.05 0.00
3 1983.0 2.86 0.40 0.00 2.14 6.93 0.78 0.00 0.00 0.48 0.00 3.20 0.00
4 1984.0 1.85 0.00 0.00 1.45 0.69 3.14 5.95 0.00 31.10 0.00 6.18 0.00
5 1985.0 3.52 0.00 1.05 0.00 43.17 3.21 0.00 0.00 1.00 0.03 1.96 0.00
6 1986.0 13.74 0.00 0.00 0.00 9.39 0.00 1.96 2.52 3.89 0.00 5.57 0.00
7 1987.0 1.12 4.38 5.42 0.00 1.74 0.00 0.00 4.65 0.71 0.00 3.72 0.00
8 1988.0 1.75 0.00 0.00 0.00 27.73 5.58 2.14 5.88 0.51 0.03 3.60 0.00
9 1989.0 4.64 0.00 0.00 1.28 20.66 37.75 0.00 2.20 1.20 0.00 5.72 0.00
10 1990.0 6.39 0.00 0.00 0.00 22.97 6.00 6.26 4.52 0.00 1.14 2.11 0.00
11 1991.0 6.76 2.24 0.39 0.08 7.64 3.24 1.14 3.25 2.00 2.14 2.41 0.94
12 1992.0 3.83 12.24 15.25 4.94 13.42 4.84 9.04 6.86 0.29 2.14 2.95 0.37
13 1993.0 1.81 0.07 8.75 0.30 18.67 3.17 0.36 5.59 3.08 0.19 3.18 0.81
14 1994.0 1.55 3.74 8.48 2.86 28.74 1.53 2.19 7.11 8.30 2.73 8.39 3.56
15 1995.0 3.57 0.72 14.85 6.40 16.69 2.71 6.09 14.26 4.15 4.18 7.98 6.51
16 1996.0 20.58 4.19 18.06 10.68 28.23 3.91 28.24 43.96 6.91 11.33 17.45 5.61
17 1997.0 27.58 4.96 11.76 5.69 21.85 5.89 31.91 21.79 22.17 9.65 30.02 7.71
18 1998.0 39.44 9.05 31.66 11.94 29.88 6.34 27.90 28.08 9.80 7.11 41.79 13.46
19 1999.0 27.77 7.60 14.26 20.65 20.82 1.36 37.27 49.09 12.25 11.44 30.29 18.45
20 2000.0 34.04 2.98 20.22 15.54 16.06 3.82 19.99 29.03 6.81 3.38 41.19 8.52
21 2001.0 59.39 9.12 18.12 16.40 39.28 8.00 55.81 22.06 24.77 19.54 51.43 7.55
22 2002.0 86.76 11.05 25.02 15.67 45.97 5.34 30.20 45.13 48.58 10.81 65.42 5.56
23 2003.0 67.93 2.14 23.73 23.82 42.89 2.42 52.19 30.26 27.13 21.19 56.11 7.99
24 2004.0 76.25 8.69 16.78 31.32 47.22 8.40 47.83 53.95 46.95 10.83 63.67 7.16
25 2005.0 85.53 8.44 19.71 61.08 23.39 20.42 56.03 28.42 41.55 48.92 59.51 5.31
26 2006.0 66.13 11.37 22.42 67.07 49.35 10.80 33.86 57.37 38.24 21.56 135.83 4.22
27 2007.0 104.97 24.10 17.49 91.52 35.10 23.74 38.87 43.45 70.67 48.42 97.78 9.26
28 2008.0 135.01 24.71 35.04 86.05 35.14 15.29 70.03 59.03 59.22 46.09 94.63 11.55
29 2009.0 137.66 20.20 31.88 75.80 40.58 19.80 33.65 47.29 69.30 33.03 137.33 12.36
30 2010.0 115.49 16.00 14.72 95.20 30.98 10.74 34.36 69.57 76.87 21.55 91.34 13.77
31 2011.0 116.76 15.61 22.37 54.45 27.71 4.95 34.83 52.85 98.17 15.13 56.12 8.84
32 2012.0 119.10 5.80 9.20 22.21 18.37 1.72 13.68 46.91 71.80 13.36 30.42 3.27
33 2013.0 122.57 6.07 7.04 25.28 24.59 0.95 12.33 44.42 62.05 8.62 41.23 6.09
34 2014.0 97.30 5.61 15.83 23.35 8.78 1.49 16.64 45.57 65.26 5.59 45.10 0.99
35 2015.0 72.04 8.15 7.92 11.56 6.06 0.72 8.07 37.66 67.49 5.64 40.90 1.84
36 2016.0 30.23 3.83 4.44 2.63 3.21 0.01 2.83 18.12 38.23 1.89 23.53 1.15
37 2017.0 0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.05 0.00 0.00 0.00 0.00
38 2020.0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.29 0.00 0.00
In [21]:
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(20, 10))

# Trazar las líneas correctamente usando plt.plot()
plt.plot(datos_temporales_wide['Year_of_Release'], datos_temporales_wide['Action'], label='Action', color='#9E0142', linestyle='--')
plt.plot(datos_temporales_wide['Year_of_Release'], datos_temporales_wide['Shooter'], label='Shooter', color='#FA9B58', linestyle='-.')
plt.plot(datos_temporales_wide['Year_of_Release'], datos_temporales_wide['Sports'], label='Sports', color='#3682BA', linestyle=':')

# Años a resaltar
years_to_highlight = [2006, 2007, 2008, 2009]

# Filtrar los datos para esos años
highlight_data = datos_temporales_wide[datos_temporales_wide['Year_of_Release'].isin(years_to_highlight)]

# Agregar puntos destacados con plt.scatter()
plt.scatter(highlight_data['Year_of_Release'], highlight_data['Action'], color='#9E0142', s=100, label='Action (Resaltado)')
plt.scatter(highlight_data['Year_of_Release'], highlight_data['Shooter'], color='#FA9B58', s=100, label='Shooter (Resaltado)')
plt.scatter(highlight_data['Year_of_Release'], highlight_data['Sports'], color='#3682BA', s=100, label='Sports (Resaltado)')


plt.xlabel('Año')
plt.ylabel('Ventas Globales')
plt.title('Ventas Globales por Género en el Tiempo')
plt.legend()
plt.show()
No description has been provided for this image

Ejercicio 4¶

Se debe escoger un género de videojuegos. Para el género escogido se debe responder:

¿Cómo es la distribución percentil? ¿dónde presenta mayor agrupamiento de los datos?
In [22]:
Action = datos[datos.Genre == 'Action']
Action
Out[22]:
Name Platform Year_of_Release Genre Publisher NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales Critic_Score Critic_Count User_Score User_Count Developer Rating percentaje_sales
16 Grand Theft Auto V PS3 2013.0 Action Take-Two Interactive 7.02 9.09 0.98 3.96 21.04 97.0 50.0 8.2 3994.0 Rockstar North M 0.235867
17 Grand Theft Auto: San Andreas PS2 2004.0 Action Take-Two Interactive 9.43 0.40 0.41 10.57 20.81 95.0 80.0 9 1588.0 Rockstar North M 0.233288
23 Grand Theft Auto V X360 2013.0 Action Take-Two Interactive 9.66 5.14 0.06 1.41 16.27 97.0 58.0 8.1 3711.0 Rockstar North M 0.182393
24 Grand Theft Auto: Vice City PS2 2002.0 Action Take-Two Interactive 8.41 5.49 0.47 1.78 16.15 95.0 62.0 8.7 730.0 Rockstar North M 0.181048
38 Grand Theft Auto III PS2 2001.0 Action Take-Two Interactive 6.99 4.51 0.30 1.30 13.10 97.0 56.0 8.5 664.0 DMA Design M 0.146856
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
16696 Metal Gear Solid V: Ground Zeroes PC 2014.0 Action Konami Digital Entertainment 0.00 0.01 0.00 0.00 0.01 80.0 20.0 7.6 412.0 Kojima Productions M 0.000112
16698 Carmageddon 64 N64 1999.0 Action Virgin Interactive 0.01 0.00 0.00 0.00 0.01 NaN NaN NaN NaN NaN NaN 0.000112
16699 Planet Monsters GBA 2001.0 Action Titus 0.01 0.00 0.00 0.00 0.01 67.0 9.0 tbd NaN Planet Interactive E 0.000112
16703 The Longest 5 Minutes PSV 2016.0 Action Nippon Ichi Software 0.00 0.00 0.01 0.00 0.01 NaN NaN NaN NaN NaN NaN 0.000112
16714 Samurai Warriors: Sanada Maru PS3 2016.0 Action Tecmo Koei 0.00 0.00 0.01 0.00 0.01 NaN NaN NaN NaN NaN NaN 0.000112

3370 rows × 17 columns

In [23]:
fig = plt.figure(figsize=(12, 6))


fig1 = fig.add_subplot(2,1,1)
fig1.boxplot([Action['Global_Sales']], labels=['Action'], 
            patch_artist=True, 
            boxprops=dict(facecolor='#9E0142', color='white'), 
            vert=False, 
            showfliers=True)

fig1.set_ylabel('Género Acción')
#fig1.set_xlabel('Ventas Globales (en millones)')
#fig1.set_title('Ventas Globales del Género Action')


fig2 = fig.add_subplot(2,1,2)
fig2.boxplot([Action['Global_Sales']], labels=['Action'], 
            patch_artist=True, 
            boxprops=dict(facecolor='#9E0142', color='white'), 
            vert=False, 
            showfliers=False)

fig2.set_ylabel('Género Acción')
fig2.set_xlabel('Ventas Globales (en millones)')
#fig2.set_title('Ventas Globales del Género Action')

plt.tight_layout()  # Ajustar el espacio entre los gráficos
plt.show()
/tmp/ipykernel_3694/244920339.py:5: MatplotlibDeprecationWarning: The 'labels' parameter of boxplot() has been renamed 'tick_labels' since Matplotlib 3.9; support for the old name will be dropped in 3.11.
  fig1.boxplot([Action['Global_Sales']], labels=['Action'],
/tmp/ipykernel_3694/244920339.py:17: MatplotlibDeprecationWarning: The 'labels' parameter of boxplot() has been renamed 'tick_labels' since Matplotlib 3.9; support for the old name will be dropped in 3.11.
  fig2.boxplot([Action['Global_Sales']], labels=['Action'],
No description has been provided for this image

En una vista superpuesta indique o compare los scores para el género escogido. ¿Hay cercanía entre las valoraciones de los usuarios y la de los críticos?

In [25]:
import plotly.graph_objects as go
import plotly.subplots as sp
import seaborn as sns
import ipywidgets as widgets
from IPython.display import display, clear_output

# Asegurar que 'Year_of_Release' sea de tipo entero y eliminar valores NaN
df_regiones = datos.dropna(subset=['Year_of_Release', 'NA_Sales', 'EU_Sales', 'JP_Sales']).copy()
df_regiones['Year_of_Release'] = df_regiones['Year_of_Release'].astype(int)

df_generos = datos.dropna(subset=['Year_of_Release', 'Genre', 'Global_Sales']).copy()
df_generos['Year_of_Release'] = df_generos['Year_of_Release'].astype(int)

# Filtrar solo los géneros de interés
generos_interes = ["Action", "Role-Playing", "Strategy", "Misc"]
df_generos = df_generos[df_generos["Genre"].isin(generos_interes)]

# Agrupar ventas por año
ventas_por_region = df_regiones.groupby("Year_of_Release")[["NA_Sales", "EU_Sales", "JP_Sales"]].sum().reset_index()
ventas_por_genero = df_generos.groupby(["Year_of_Release", "Genre"])["Global_Sales"].sum().reset_index()

# Widget para seleccionar el rango de años
year_range = widgets.IntRangeSlider(
    value=[ventas_por_region["Year_of_Release"].min(), ventas_por_region["Year_of_Release"].max()],
    min=ventas_por_region["Year_of_Release"].min(),
    max=ventas_por_region["Year_of_Release"].max(),
    step=1,
    description='Años:',
    continuous_update=False
)

# Función para actualizar ambas gráficas
def actualizar_graficos(year_range):
    start_year, end_year = year_range  # Obtener el rango de años seleccionado
    df_region_filtrado = ventas_por_region[(ventas_por_region["Year_of_Release"] >= start_year) &
                                           (ventas_por_region["Year_of_Release"] <= end_year)]

    df_genero_filtrado = ventas_por_genero[(ventas_por_genero["Year_of_Release"] >= start_year) &
                                           (ventas_por_genero["Year_of_Release"] <= end_year)]

    # Limpiar salida y mostrar el selector de rango
    clear_output(wait=True)

    # Definir colores de la paleta Spectral
    colores = sns.color_palette("Spectral", 7).as_hex()
    colores_regiones = {"NA_Sales": colores[0], "EU_Sales": colores[1], "JP_Sales": colores[2]}
    colores_generos = {"Action": colores[3], "Role-Playing": colores[4],
                       "Strategy": colores[5], "Misc": colores[6]}

    # Crear subplot con 2 filas y 1 columna
    fig = sp.make_subplots(rows=2, cols=1,
                           subplot_titles=("Tendencia de Ventas por Región",
                                           "Tendencia de Ventas por Género"))

    # Gráfica 1: Tendencia de ventas por región
    for region in ["NA_Sales", "EU_Sales", "JP_Sales"]:
        fig.add_trace(go.Scatter(
            x=df_region_filtrado["Year_of_Release"],
            y=df_region_filtrado[region],
            mode='lines+markers',
            name={"NA_Sales": "EE.UU", "EU_Sales": "Europa", "JP_Sales": "Japón"}[region],
            line=dict(color=colores_regiones[region], width=2),
            marker=dict(size=6),
            hovertemplate=f"<b>{region}</b><br>Año: %{{x}}<br>Ventas: %{{y:.2f}}M USD<extra></extra>"
        ), row=1, col=1)

    # Gráfica 2: Tendencia de ventas por género
    for genero in generos_interes:
        df_genero = df_genero_filtrado[df_genero_filtrado["Genre"] == genero]
        fig.add_trace(go.Scatter(
            x=df_genero["Year_of_Release"],
            y=df_genero["Global_Sales"],
            mode='lines+markers',
            name=genero,
            line=dict(color=colores_generos[genero], width=2),
            marker=dict(size=6),
            hovertemplate=f"<b>{genero}</b><br>Año: %{{x}}<br>Ventas: %{{y:.2f}}M USD<extra></extra>"
        ), row=2, col=1)

    # Personalizar diseño de la gráfica
    fig.update_layout(
        title=dict(
            text="Tendencia de Ventas en Regiones y Géneros",
            font=dict(family="serif", size=22, color="black", weight="bold"),  # Fuente y tamaño del título
            x=0.5  # Centrar el título
        ),
        plot_bgcolor='white',
        height=800,
        legend=dict(
            title_text="Categoría",
            font=dict(family="serif", size=14, color="black", weight="normal")  # Fuente y tamaño de la leyenda
        )
    )

    # Ajustes individuales para cada eje X e Y con fuentes personalizadas
    fig.update_xaxes(title="Año", tickfont=dict(family="serif", size=16, color="black", weight="bold"), row=1, col=1)
    fig.update_xaxes(title="Año", tickfont=dict(family="serif", size=16, color="black", weight="bold"), row=2, col=1)
    fig.update_yaxes(title="Ventas (Millones USD)", tickfont=dict(family="serif", size=16, color="black", weight="bold"), row=1, col=1)
    fig.update_yaxes(title="Ventas (Millones USD)", tickfont=dict(family="serif", size=16, color="black", weight="bold"), row=2, col=1)

    # Mostrar la gráfica
    fig.show()

# Mostrar el selector de años y la gráfica inicial
display(year_range)
actualizar_graficos(year_range.value)

# Conectar el widget al cambio de selección
year_range.observe(lambda change: actualizar_graficos(change["new"]), names='value')

¿Cuál es el valor del score aproximadamente donde un videojuego alcanzó el mayor número de ventas?

In [ ]:
 

¿Cuál es la distribución de la correlación para el género escogido?

In [ ]:
 
In [ ]:
¿Qué relación existe entre los scores de los jugadores y de la crítica para el género escogido?
In [ ]: